package com.atguigu.flink.chapter11;

import com.atguigu.flink.chapter05.Source.WaterSensor;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

import static org.apache.flink.table.api.Expressions.$;

/**
 * TODO
 *
 * @author cjp
 * @version 1.0
 * @date 2021/1/27 14:19
 */
public class Flink09_TimeAttr_ProcTime {
    public static void main(String[] args) {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        DataStreamSource<WaterSensor> sensorDS =
                env.fromElements(
                        new WaterSensor("sensor_1", 1000L, 10),
                        new WaterSensor("sensor_1", 2000L, 20),
                        new WaterSensor("sensor_2", 3000L, 30),
                        new WaterSensor("sensor_1", 4000L, 40),
                        new WaterSensor("sensor_1", 5000L, 50),
                        new WaterSensor("sensor_2", 6000L, 60));

        // 1.创建 表的 执行环境
        StreamTableEnvironment tableEnv = StreamTableEnvironment.create(env);

        // TODO 方式一:在 流 转换成 表的时候，指定 处理时间
        // 注意：处理时间，只能 在 提供的 额外字段上指定
//        Table inputTable = tableEnv.fromDataStream(sensorDS, $("id"), $("ts"), $("vc"), $("pt").proctime());

//        inputTable
//                .execute()
//                .print();


        // TODO 方式二：在 使用 SQL 创建表 的时候指定
        tableEnv.executeSql("create table sensor(id string,ts bigint,vc int,pt_time as PROCTIME()) with("
                + "'connector' = 'filesystem',"
                + "'path' = 'input/sensor-sql.txt',"
                + "'format' = 'csv'"
                + ")");

        tableEnv.executeSql("select * from sensor").print();

    }
}
